/**
 * ws类公用方法
 */

const RoomData = require("../room/room-data");
const log = require("./log");
const CURD = require('../compontents/DB/CURDClass')

const baseWS = {
    async send (socket, msg, uid) {
        // 联合查询数据库：角色
        const fields = 'U.ename, UINFO.cname, UINFO.age';
        CURD.other(`select ${fields} from xhpx_user U left join xhpx_user_info UINFO on U.uid = UINFO.uid where U.uid = '${uid}'`).then((roleInfo) => {
            // 组装数据
            new RoomData().get(msg, roleInfo[0])
                .then(async (result) => {
                    console.log(result);
                    // 角色面板
                    try {
                        // 查询本地：NPC
                        const npcName = result.npc.name;
                        if (npcName) {
                            npcName.map((item) => {
                                const npcInfo = require(`../room/${result.npc.path}/npc/${item}.json`)
                                npcInfo.type = 'npc'
                                socket.send(JSON.stringify({
                                    result: npcInfo
                                }));
                            })
                        }

                        // 界面数据
                        socket.send(JSON.stringify({
                            result: result
                        }));
                    } catch (err) {
                        log.err(`找不到npc文件：${err}`)
                    }
                });
        });
        // if (roleInfo.length) {
            // roleInfo[0].type = 'npc'
            // socket.send(JSON.stringify({
            //     result: roleInfo[0]
            // }));
        // }


    }
}
module.exports = baseWS;